package com.atguigu.controller;

import com.atguigu.dao.EmployeeDao;
import com.atguigu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;

/**
 * @author 林允
 * @version 1.0
 * @description: TODO
 * @date 2023/5/15 19:36
 * 查询所有的员工信息 --> /employee --> GET
 * 跳转到添加页面 --> /to/add --> GET
 * 添加员工信息 --> /employee --> POST
 * 跳转到修改页面 --> /employee/1 --> GET
 * 修改员工信息 --> /employee --> PUT
 * 删除员工信息 --> /employee/1 --> DELETE
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String getAllEmployee(Model model){
        // 获取所有的员工信息
        Collection<Employee> allEmployee = employeeDao.getAll();
        // 将所有的员工信息在请求域中共享
        model.addAttribute("allEmployee", allEmployee);
        // 跳转到列表页面
        return "employee_list";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.POST)
    public String addEmployee(Employee employee){
        // 保存员工信息
        employeeDao.save(employee);
        // 重定向到列表功能：/employee 再次调用getAllEmployee
        return "redirect:/employee";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        // 根据id查询员工信息
        Employee employee = employeeDao.get(id);
        // 将员工信息共享到请求域中
        model.addAttribute("employee", employee);
        // 跳转到employee_update.html
        return "employee_update";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee){
        // 修改员工信息
        employeeDao.save(employee);
        // 重定向
        return "redirect:/employee";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id){
        // 删除员工信息
        employeeDao.delete(id);
        // 重定向
        return "redirect:/employee";
    }

}
